package com.example.demo.controller;

import com.example.demo.Expection.ApplicationException;
import com.example.demo.comment.AppResult;
import com.example.demo.model.User;
import com.example.demo.service.imp.UserService;
import com.example.demo.until.Md5Until;
import com.example.demo.until.UUIDUntil;
import jdk.management.resource.internal.inst.SocketOutputStreamRMHooks;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    public UserService userService;

    @RequestMapping("/login")
    public AppResult login(@RequestParam(value = "username",required = true) String username,
                           @RequestParam(value = "password",required = true) String password,
                           HttpServletRequest request) throws ApplicationException {
        System.out.println("11111");
               if(username==null||!StringUtils.hasLength(username)||password==null||!StringUtils.hasLength(password)){
                 throw new ApplicationException("参数校验失败");
               }

              User user = userService.selectUserByName(username);
        System.out.println("131313113");
              if(user==null||user.getId()<=0){
                  System.out.println("ggoooddd");
                  throw new ApplicationException("用户不存在");
              }
              String  salt = user.getSalt();
        System.out.println("盐值为："+ salt);
        System.out.println(user.getPassword());
        String s3 = Md5Until.setLastMd5(password,salt);
        System.out.println(s3);
              if(!Md5Until.setLastMd5(password,salt).equals(user.getPassword())){
                  return AppResult.failed("密码不正确");
              }

        HttpSession session = request.getSession(true);
              session.setAttribute("userInfo",user);
              return AppResult.success();

    }

    @RequestMapping(method = RequestMethod.POST,value = "/register")
    public AppResult register(@RequestParam String username,
                              @RequestParam String nickname,
                              @RequestParam String password,
                              @RequestParam(value = "passwordRepeat") String passwordRepeat) throws ApplicationException {

        if(username==null||!StringUtils.hasLength(username)||nickname==null||!StringUtils.hasLength(nickname)
        ||password==null||!StringUtils.hasLength(password)||!StringUtils.hasLength(passwordRepeat)||passwordRepeat==null||
        !passwordRepeat.equals(password)){
            System.out.println("123333");
          throw new ApplicationException("参数校验异常");
        }
        User user = userService.selectUserByName(username);
        System.out.println(user);
        if(user!=null){
           throw new ApplicationException("该用户名已经存在");
        }

        User user1 = new User();
        user1.setUsername(username);
       String salt =  UUIDUntil.getUUID_32();
       String mi = Md5Until.setLastMd5(password,salt);
       user1.setPassword(mi);

        user1.setNickname(nickname);
        user1.setSalt(salt);
        user1.setUpdateTime(new Date());
        user1.setDeleteState((byte)0);
        user1.setCreateTime(new Date());
        user1.setIsAdmin((byte) 0);
        user1.setArticleCount(0);
        user1.setState((byte) 0);
        System.out.println(user1);
       userService.insertUser(user1);
       return AppResult.success("插入成功");
    }

}
